var Role = {
    table: null,
    layerIndex: -1
};

Role.init = function () {
    layui.form.render();
    var me = this;
    this.table = new LayUITable("table", '/system/sysRole/list', this.initColumns());
    me.table.params = {
        height: $(".dawn-content").height() - 50
    }
    me.table.setQueryParams(me.queryParams);
    me.table.handleListenToolBar = function (event, checkStatus, obj) {
        switch (event) {
            case "add":
                layui.laytpl($("#info").html()).render({}, function (html) {
                    Role.itemIndex = layer.open({
                        type: 1,
                        offset: 'r',
                        anim: 'slideLeft', // 从右往左
                        shade: 0.1,
                        shadeClose: true,
                        title: '添加角色',
                        area: ['500px', '100%'], //宽高
                        content: html,
                        success: function () {
                            Role.add();
                        }
                    });
                });
                break;
        }
    };
    me.table.handleListenTool = function (event, data, obj) {
        switch (event) {
            case "edit":
                layui.laytpl($("#info").html()).render({}, function (html) {
                    Role.itemIndex = layer.open({
                        type: 1,
                        offset: 'r',
                        anim: 'slideLeft', // 从右往左
                        shade: 0.1,
                        shadeClose: true,
                        title: '修改角色',
                        area: ['500px', '100%'], //宽高
                        content: html,
                        success: function () {
                            Role.update(data.id);
                        }
                    });
                });
                break;
            case "delete":
                var operation = function(){
                    $request.get("/system/sysRole/remove", function (res) {
                        if(res.success) {
                            Dawn.success("删除成功!");
                            Role.table.refresh();
                        } else {
                            Dawn.error(res.message);
                        }
                    }).set("roleId", data.id).start();
                };

                Dawn.deleteConfirm("是否删除角色 " + data.name + "?",operation);
                break;
            case "setAuthority":
                layui.laytpl($("#assign").html()).render({}, function (html) {
                    Role.itemIndex = layer.open({
                        type: 1,
                        offset: 'r',
                        anim: 'slideLeft', // 从右往左
                        shade: 0.1,
                        shadeClose: true,
                        title: '权限配置',
                        area: ['300px', '100%'], //宽高
                        content: html,
                        success: function () {
                            Role.assign(data.id);
                        }
                    });
                });
                break;
        }
    };
    me.table.init();
    this.addEvents();
};

Role.addEvents = function () {
    var me = this;
    $("#search").click(function () {
        me.search();
    })
    $("#reset").click(function () {
        me.reset();
    });
};

Role.assign = function(id) {
    $("#btn_close").unbind().bind("click", function () {
        layui.layer.close(Role.itemIndex);
    });
    $("#btn_save").unbind().bind("click", function () {
        var ids = Dawn.zTreeCheckedNodes("zTree");
        $request.postJson("/system/sysRole/setAuthority", function (data) {
            if(data.success) {
                Dawn.success("分配角色成功!");
                Role.table.refresh();
                layui.layer.close(Role.itemIndex);
            } else {
                Dawn.error(data.message);
            }
        }).set("roleId", id).set("ids", ids).start();
    });
    var setting = {
        check: {
            enable: true,
            chkboxType: {"Y": "ps", "N": "ps"}
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    var ztree = new $ZTree("zTree", "/system/sysMenu/menuTreeListByRoleId?roleId=" + id);
    ztree.setSettings(setting);
    ztree.init();
};

Role.update = function(id) {
    $request.get("/system/sysRole/info", function(data) {
        if(data.success) {
            layui.form.val("info", data.data.role);
            layui.form.render();
            $("#pName").val(data.data.pName == '' ? "顶级" : data.data.pName);
            Role.initZTree();
            layui.form.on('submit(save)', function (data) {
                $request.postJson("/system/sysRole/edit", function (res) {
                    if (res.success) {
                        Dawn.success("更新成功!");
                        Role.table.refresh();
                        layui.layer.close(Role.itemIndex);
                    } else {
                        Dawn.error(res.message);
                    }
                }).setData(data.field).start();
                return false;
            });
        } else {
            Dawn.error(data.message);
        }
    }).setData({
        roleId: id
    }).setAsync(false).start();
};

Role.add = function() {
    layui.form.render();
    Role.initZTree();
    layui.form.on('submit(save)', function (data) {
        $request.postJson("/system/sysRole/add", function (res) {
            if (res.success) {
                Dawn.success("保存成功!");
                Role.table.refresh();
                layui.layer.close(Role.itemIndex);
            } else {
                Dawn.error(res.message);
            }
        }).setData(data.field).start();
        return false;
    });
};

Role.initZTree = function() {
    var ztree = new $ZTree("pNameTree", "/system/sysRole/roleTreeList");
    ztree.bindOnClick(Role.onClickDept);
    ztree.init();
    $("#pName").unbind();
    $("#pName").click(function () {
        if($("#pNameContent").is(":hidden")) {
            Dawn.showInputTree("pNameContent");
        } else {
            Dawn.hideInputTree("pNameContent");
        }

    });
};

Role.onClickDept = function(e, treeId, treeNode) {
    $("#pName").val(treeNode.name);
    $("input[name='pid']").val(treeNode.id);
    Dawn.hideInputTree("pcodeTreeDiv");
};

Role.reset = function () {
    $("#roleName").val("");
    layui.form.render();
    this.search();
}

Role.search = function () {
    this.table.init();
}

Role.queryParams = function () {
    var queryData = {};
    queryData['condition'] = $("#roleName").val();
    return queryData;
}

Role.initColumns = function () {
    return [
        [
            {
                field: 'id',
                title: 'ID',
                align: 'center',
                hide: true
            },
            {
                field: 'name',
                title: '角色名称',
                align: 'center',
                sort: true
            },
            {
                field: 'code',
                title: '角色编码',
                align: 'center',
                sort: true
            }, {
            field: 'num',
            title: '排序',
            align: 'center',
            sort: true
        }, {
            field: 'pName',
            title: '上级角色',
            align: 'center'
        }, {
            fixed: 'right',
            title: '操作',
            width: "200",
            toolbar: '#tableTool',
            align: 'center'
        }
        ]
    ];
}


$(function () {
    layui.element.init();
    Role.init();
});
